<template>
  <el-dialog :visible.sync="isShow" title="分配权限">
    <!-- [{label,children}] 
    check-strictly:boolean,true:父子选择不关系
    tree默认选中：
       1：node-key:设置对应数据中某个字段名（常用于id）
       2:设置默认选中项 default-checked-keys=[node-key对应的字段数据],只用于第一次设置
     getCheckedKeys:它是el-tree身上的methods内的一个方法，它能获取先中项的node-key对应字段的集合
     setCheckedKeys([希望选中项的对应的node-key对应的值]):设置相应选中项
    
    -->
    <h3>{{ defaultArr }}</h3>
    <el-tree
      check-strictly
      ref="tree"
      node-key="id"
      :default-checked-keys="defaultArr"
      :data="treeData"
      :props="{ label: 'name' }"
      show-checkbox
    ></el-tree>
    <template #footer>
      <div>
        <el-button @click="closeEvent">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { sysPermission } from '@/api/permission'
import { sysRoleGet, sysRolePut } from '@/api/settings'
export default {
  data () {
    return {
      isShow: false,
      treeData: [],
      defaultArr: [],
      info: ''
    }
  },
  created () {
    this.getData()
  },
  methods: {
    async getData () {
      const res = await sysPermission()
      window.console.log(res)
      this.treeData = this.changeData(res)
    },
    changeData (arr, pid = '0') {
      return arr.filter(item => {
        if (item.pid === pid) {
          item.children = this.changeData(arr, item.id)
          return true
        }
      })
    },
    // 获取当前点击项所对应的权限数据
    async getRoleInfo (id) {
      const _info = await sysRoleGet(id)
      this.info = _info
      this.$refs.tree.setCheckedKeys(_info.permIds)
      //   this.defaultArr = _info.permIds
      window.console.log('_info', _info)
    },
    closeEvent () {
      this.isShow = false
      this.$refs.tree.setCheckedKeys([])
    },
    // 确定按钮点击
    async submit () {
      this.info.permIds = this.$refs.tree.getCheckedKeys()
      await sysRolePut(this.info)
      this.$message.success('设置权限成功')
      this.closeEvent()
      this.$emit('getData')
    }
  }
}
</script>
<style></style>
